package com.app.worldcupapp.list;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.Looper;
import android.text.Layout;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.app.worldcupapp.R;
import com.app.worldcupapp.adapter.MyListAdapter;
import com.app.worldcupapp.bean.LoginBean;
import com.app.worldcupapp.bean.TeamsBean;
import com.app.worldcupapp.bean.TeamsDetailInfo;
import com.app.worldcupapp.listdetalil.TeamDetailActivity;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;
import java.util.Map;


import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class TeamsListActivity extends AppCompatActivity {
TextView teamId,teamNameId;
String TAG = "TeamsListActivity";
ListView listViewId;
ImageView picId;
TeamsBean teamsBean;
TabLayout tab;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teams_list);


        initView();
        initData();
        getData();
        tab.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()){
                    case 1:
                        Intent intent = new Intent(TeamsListActivity.this,TeamBoardActivity.class);
                        startActivity(intent);
                        finish();
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

listViewId.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String i =String.valueOf(teamsBean.getList().get(position).getId());
        //创建Client
        OkHttpClient client = new OkHttpClient();
        RequestBody body = new FormBody.Builder()
                .add("id",i)
                .build();
        //创建Request对象
        final Request request = new Request.Builder()
                .url("http:/192.168.27.206:8080/app19/teams/findeone")
                .post(body)
                .build();

        final Call call = client.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG,"网络访问失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String data = response.body().string();
                Gson gson = new Gson();
                LoginBean result = gson.fromJson(data, LoginBean.class);

                if (result.isSuccess()){
                    Intent intent = new Intent(TeamsListActivity.this, TeamDetailActivity.class);
                    // 设置你要传的参数
                    intent.putExtra("id", i);
                    startActivity(intent);
                }else {
                    Looper.prepare();
                    Toast.makeText(TeamsListActivity.this, "列表详细信息不存在", Toast.LENGTH_SHORT).show();
                    Looper.loop();
                }
            }
        });

    }
});

    }

    private void pageSwitch() {
    }


    private void getData() {
        //创建Client
        OkHttpClient client = new OkHttpClient();
        RequestBody body = new FormBody.Builder()
                .build();
        //创建Request对象
        final Request request = new Request.Builder()
                .url("http://192.168.27.206:8080/app18/teams/list")
                .post(body)
                .build();

        final Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG,"网络访问失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String data = response.body().string();
                Gson gson = new Gson();
                 teamsBean = gson.fromJson(data, TeamsBean.class);
                if (teamsBean.isSuccess()) {
                    // 获取链表数据
                     List list = teamsBean.getList();
                    //使用runOnUITHread才能获取到
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // 构建适配器对象
                            MyListAdapter adapter = new MyListAdapter(list, getApplicationContext());
                            // 为listview设置适配器对象
                            listViewId.setAdapter(adapter);
                        }
                    });


                } else {
                    Toast.makeText(getApplicationContext(), "失败", Toast.LENGTH_SHORT).show();
                }



            }
        });
    }

    private void initData() {
    }

    private void initView() {
        tab = findViewById(R.id.listTabId);
        listViewId = findViewById(R.id.listView);
        teamId = findViewById(R.id.teamId);
        teamNameId = findViewById(R.id.teamNameId);
        picId = findViewById(R.id.picId);

    }


}